#include "addalarmdialog.h"
#include "ui_addalarmdialog.h"

#include <QMessageBox>

AddAlarmDialog::AddAlarmDialog(QOpcUaClient *client, QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::AddAlarmDialog)
    , m_client(client) {
    ui->setupUi(this);
    this->setWindowTitle("添加要监视的点位");
    qInfo() << "进入'添加监视节点'页面";
    ui->stackedWidget_condition->setCurrentIndex(0);

    nodeSelDialog = new OpcUaNodeSelect(m_client, this);

    connect(ui->lineEdit_broweName, &QLineEdit::textChanged, this, &AddAlarmDialog::dataChanged);
    connect(ui->spinBox_interval, &QSpinBox::valueChanged, this, &AddAlarmDialog::dataChanged);
    connect(ui->dSB_num, &QDoubleSpinBox::valueChanged, this, &AddAlarmDialog::dataChanged);
    connect(ui->cbB_num_condition, &QComboBox::currentIndexChanged, this, &AddAlarmDialog::dataChanged);
    connect(ui->rB_true, &QRadioButton::clicked, this, &AddAlarmDialog::dataChanged);
    connect(ui->rB_false, &QRadioButton::clicked, this, &AddAlarmDialog::dataChanged);
}

AddAlarmDialog::~AddAlarmDialog() {
    delete nodeSelDialog;
    delete ui;
}

void AddAlarmDialog::on_btn_addOpcUaNode_clicked() {
    if(nodeSelDialog->exec() == QDialog::Accepted) {
        QString tmp_str = nodeSelDialog->getValue(Qt::UserRole + 6);   // description
        if(tmp_str.isEmpty())
            tmp_str = nodeSelDialog->getValue(Qt::DisplayRole);
        ui->lineEdit_broweName->setText(tmp_str); // browseName
        ui->lineEdit_nodeId->setText(nodeSelDialog->getValue(Qt::UserRole + 4));   // nodeId
        ui->lineEdit_dataType->setText(nodeSelDialog->getValue(Qt::UserRole + 3)); // dataType
        if(nodeSelDialog->getValue(Qt::UserRole + 3) == "Boolean")
            ui->stackedWidget_condition->setCurrentIndex(1);
        else
            ui->stackedWidget_condition->setCurrentIndex(0);
    }
}

QString AddAlarmDialog::getValue(int role) {
    switch (role) {
    case Qt::DisplayRole:
        return ui->lineEdit_broweName->text();
    case Qt::UserRole + 1:// value
        return nodeSelDialog->getValue(Qt::UserRole + 1);
    case Qt::UserRole + 2:// nodeClass
        return nodeSelDialog->getValue(Qt::UserRole + 2);
    case Qt::UserRole + 3:// dataType
        return ui->lineEdit_dataType->text();
    case Qt::UserRole + 4:// nodeId
        return ui->lineEdit_nodeId->text();
    case Qt::UserRole + 5:// condition 条件
        if(ui->lineEdit_dataType->text() == "Boolean")
            return "=";
        else {
            return ui->cbB_num_condition->currentText();
        }
    case Qt::UserRole + 6:// cond_value 条件值
        if(ui->lineEdit_dataType->text() == "Boolean")
            return ui->rB_true->isChecked() ? "true" : "false";
        else
            return ui->dSB_num->text();
    case Qt::UserRole + 7:// 语音
        return ui->voiceText->toPlainText();
    case Qt::UserRole + 8:// 刷新时间
        return ui->spinBox_interval->text();
    default:
        return NULL;
    }
}

void AddAlarmDialog::on_btn_add_clicked() {
    QString browName  = ui->lineEdit_broweName->text();
    QString nodeId    = ui->lineEdit_nodeId->text();
    QString condition = ui->lineEdit_dataType->text() == "Boolean" ? "=" : ui->cbB_num_condition->currentText();
    QString condValue = ui->lineEdit_dataType->text() == "Boolean" ? (ui->rB_true->isChecked() ? "true" : "false") : ui->dSB_num->text();
    QString voiceTxt  = ui->voiceText->toPlainText();
    QString interval  = ui->spinBox_interval->text();
    if(browName.isEmpty() || nodeId.isEmpty()) {
        QMessageBox::warning(this, "提示", "错误：【项目名】【节点Id】为必填项！");
        return;
    }
    emit addItem(false, browName,  nodeId,  condition,  condValue,  voiceTxt, interval, false);
    ui->btn_add->setEnabled(false);
}

void AddAlarmDialog::dataChanged() {
    ui->btn_add->setEnabled(true);
}


void AddAlarmDialog::on_btn_close_clicked() {
    close();
}

